"""
向量数据模型
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from pgvector.sqlalchemy import Vector

from app.database import Base


class Embedding(Base):
    """向量模型"""
    __tablename__ = "embeddings"

    id = Column(Integer, primary_key=True, index=True, comment="向量ID")
    diary_id = Column(Integer, ForeignKey("diaries.id"), nullable=False, comment="日记ID")
    content_chunk = Column(Text, nullable=False, comment="文本片段")
    embedding = Column(Vector(1024), nullable=False, comment="向量数据")  # text-embedding-v4默认维度为1024
    chunk_index = Column(Integer, default=0, comment="片段索引")
    
    # 时间字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    
    # 关联关系
    diary = relationship("Diary", back_populates="embeddings")

    def __repr__(self):
        return f"<Embedding(id={self.id}, diary_id={self.diary_id}, chunk_index={self.chunk_index})>" 